<?php
class wdingClassAction extends Action
{
    public function setsaveAjax()
    {
        m('dingding:index')->clearalltoken();
        $pid = m('dingding:index')->optionpid;
        $this->option->setval('dingding_corpid@' . $pid . '', $this->post('corpid'));
        $this->option->setval('dingding_secret@' . $pid . '', $this->post('secret'));
        $this->option->setval('dingding_huitoken@' . $pid . '', $this->post('huitoken'));
        $this->option->setval('dingding_aeskey@' . $pid . '', $this->post('aeskey'));
        $this->backmsg();
    }
    public function getsetAjax()
    {
        $arr = array();
        $arr['corpid'] = $this->option->getval('dingding_corpid');
        $arr['secret'] = $this->option->getval('dingding_secret');
        $arr['huitoken'] = $this->option->getval('dingding_huitoken');
        $arr['aeskey'] = $this->option->getval('dingding_aeskey');
        $arr['huiurl'] = '' . URL . 'api.php?m=ding';
        echo json_encode($arr);
    }
    public function testsendAjax()
    {
        $lx = (int) $this->get('lx');
        if ($lx == 0) {
            $val = m('dingding:index')->getticket();
        } else {
            $val = 'ok';
        }
        if ($val == '') {
            showreturn('', '测试失败');
        } else {
            showreturn('', '测试成功');
        }
    }
    public function updatedeptAjax()
    {
        $arr = m('dingding:dept')->getdeptlist();
        $this->returnjson($arr);
    }
    public function optdeptAjax()
    {
        $ids = $this->get('ids');
        $db = m('dingding:dept');
        $oi = $db->rows("`bmid`='{$ids}'");
        $rs = $this->db->getone('[Q]dept', 'id=' . $ids . '');
        if ($oi == 0) {
            $barr = $db->createdept($rs['id'], $rs['name'], $rs['pid'], $rs['sort']);
        } else {
            $barr = $db->updatedept($rs['id'], $rs['name'], $rs['pid'], $rs['sort']);
        }
        $this->returnjson($barr);
    }
    public function deletedeptAjax()
    {
        $ids = $this->get('ids');
        $arr = m('dingding:dept')->deletedept($ids);
        $this->returnjson($arr);
    }
    public function deptwxdataAjax()
    {
        $this->rows = array();
        $this->getdeptwx(0, 1);
        $this->returnjson(array('totalCount' => 0, 'rows' => $this->rows));
    }
    private function getdeptwx($pid, $oi)
    {
        $db = m('zding_dept');
        $menu = $db->getall("`parentid`='{$pid}' order by `order`,`bmid`", '*');
        foreach ($menu as $k => $rs) {
            $sid = $rs['id'];
            $rs['level'] = $oi;
            $rs['stotal'] = $db->rows("`parentid`='{$sid}'");
            $zt = $this->db->rows('[Q]dept', 'id=' . $rs['bmid'] . '');
            $rs['zt'] = $zt;
            $this->rows[] = $rs;
            $this->getdeptwx($sid, $oi + 1);
        }
    }
    public function deptdataAjax()
    {
        $this->rows = array();
        $this->getdept(0, 1);
        $this->returnjson(array('totalCount' => 0, 'rows' => $this->rows));
    }
	
	
    private function getdept($pid, $oi)
    {
        $db = m('dept');
        $menu = $db->getall("`pid`='{$pid}' order by `sort`", '*');
        foreach ($menu as $k => $rs) {
            $sid = $rs['id'];
            $rs['level'] = $oi;
            $rs['stotal'] = $db->rows("`pid`='{$sid}'");
            $zt = $this->db->rows('[Q]zding_dept', "`bmid`='{$sid}' and `name`='" . $rs['name'] . "'");
            $rs['zt'] = $zt;
            $this->rows[] = $rs;
            $this->getdept($sid, $oi + 1);
        }
    }
	
	
    public function anaytodeptAjax()
    {
        $arr = m('dingding:dept')->anaytosys();
        $this->returnjson($arr);
    }
	
	
    public function beforeusershow($table)
    {
        $fields = 'id,name,`user`,deptname,status,tel,ranking,superman,loginci,deptid,sex,mobile,email,weixinid,sort,face';
        $s = '';
        $key = $this->post('key');
        $deptid = (int) $this->post('deptid', 0);
        if ($key != '') {
            $s = " and (`name` like '%{$key}%' or `user` like '%{$key}%' or `ranking` like '%{$key}%' or `mobile` like '{$key}%' or `tel` like '{$key}%' or `deptname` like '%{$key}%') ";
        }
        if ($deptid > 1) {
            $s .= " and `deptpath` like '%[" . $deptid . "]%'";
        }
        return array('fields' => $fields, 'where' => $s);
    }
    public function beforeuserdshow($table)
    {
        $s = '';
        $key = $this->post('key');
        $deptid = (int) $this->post('deptid', 0);
        if ($key != '') {
            $s = " and (`name` like '%{$key}%' or `userid` like '%{$key}%' or `position` like '%{$key}%' or `mobile` like '{$key}%' or `tel` like '{$key}%') ";
        }
        if ($deptid > 1) {
            $s .= " and `department` like '%[" . $deptid . "]%'";
        }
        return array('where' => $s);
    }
    public function afterusershow($table, $rows)
    {
        $db = m('dingding:user');
        foreach ($rows as $k => $rs) {
            $iscj = 0;
            $isgz = 0;
            $where = '';
            if (!isempt($rs['mobile'])) {
                $where .= " or `mobile`='" . $rs['mobile'] . "'";
            }
            if (!isempt($rs['email'])) {
                $where .= " or `email`='" . $rs['email'] . "'";
            }
            $urs = $db->getone("1=1 and (`userid`='" . $rs['user'] . "' {$where})");
            $cjid = 0;
            if ($urs) {
                $iscj = 1;
                if (arrvalue($urs, 'status') == 1) {
                    $isgz = 1;
                }
                $cjid = $urs['id'];
            }
            $rows[$k]['cjid'] = $cjid;
            $rows[$k]['iscj'] = $iscj;
            $rows[$k]['isgz'] = $isgz;
            $rows[$k]['isgc'] = $db->isgeng($urs, $rs);
        }
        $noarr = $db->notinadmin();
        return array('rows' => $rows, 'notstr' => join(',', $noarr));
    }
    public function reloaduserAjax()
    {
        $id = $this->get('id', 1);
        $arr = m('dingding:user')->getuserlist($id);
        $this->returnjson($arr);
    }
    public function optuserAjax()
    {
        $id = (int) $this->post('id');
        $cjid = (int) $this->post('cjid');
        $barr = m('dingding:user')->optuserwx($id, $cjid);
        $this->returnjson($barr);
    }
    public function delalluserAjax()
    {
        $arr = m('dingding:user')->delusernoinstr();
        $this->returnjson($arr);
    }
    public function delduserAjax()
    {
        $id = (int) $this->post('id', '0');
        $dbs = m('dingding:user');
        $userid = $dbs->getmou('userid', $id);
        $arr = $dbs->deleteuser($userid);
        if ($arr['errcode'] != 0) {
            $this->showreturn('', $arr['msg'], 201);
        }
        $this->showreturn('');
    }
    public function anaytouserAjax()
    {
        $arr = m('dingding:user')->anaytosys();
        $this->returnjson($arr);
    }
    public function sendagentAjax()
    {
        $name = $this->post('name');
        $msg = $this->post('msg');
        $barr = m('dingding:index')->sendtext($this->adminid, $name, $msg);
        $this->returnjson($barr);
    }
    public function delyingAjax()
    {
        $id = (int) $this->post('id');
        m('zding_agent')->delete($id);
        $this->backmsg();
    }
    public function getchatlistAjax()
    {
        $barr = m('weixin:chat')->getchatlist();
        $this->returnjson($barr);
    }
    public function getchatinfoAjax()
    {
        $barr = m('weixin:chat')->getchatinfo($this->get('chatid'));
        $this->returnjson($barr);
    }
    public function aftechatshow($table, $rows)
    {
        $db = m('weixin:chat');
        foreach ($rows as $k => $rs) {
            $zt = $db->rows($rs['id']);
            $rows[$k]['zt'] = $zt;
            $rows[$k]['sontts'] = m('im_groupuser')->rows("gid='" . $rs['id'] . "'");
        }
        return array('rows' => $rows);
    }
    public function createchatAjax()
    {
        $id = (int) $this->get('id');
        $zt = m('wx_chat')->rows($id);
        if ($zt == 0) {
            $barr = m('weixin:chat')->chatcreate($id);
        } else {
            $barr = m('weixin:chat')->chatupdate($id);
        }
        $this->returnjson($barr);
    }
    public function sendchatAjax()
    {
        $gid = (int) $this->post('gid');
        $cont = $this->post('msg');
        $barr = m('weixin:chat')->send($this->adminid, 'group', $gid, $cont);
        $this->returnjson($barr);
    }
}